package com.czy.owner.ui.store;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.entity.StoreDetailInfoModel;
import com.czy.owner.permission.Acp;
import com.czy.owner.permission.AcpListener;
import com.czy.owner.permission.AcpOptions;
import com.czy.owner.utils.AMapUtil;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.widget.UniversalDialog;
import com.czy.owner.widget.WifiDialog;

import java.util.List;

/**
 * Created by wangyang on 2017/5/19 0019.
 */

public class StoreUtils {
    //适配6.0的动态权限管理//打开位置权限
    public static void onClickLocation(final Context ctx, final StoreDetailInfoModel.StoreInfoBean infoModel) {
        Acp.getInstance(ctx).request(new AcpOptions.Builder().setPermissions(Manifest.permission.ACCESS_FINE_LOCATION).build(),
                new AcpListener() {
                    @Override
                    public void onGranted() {
                        try {
                            if (AMapUtil.isInstallByRead("com.autonavi.minimap")) {
                                AMapUtil.goToNaviActivity(ctx, ctx.getString(R.string.app_name), null, infoModel.getGprsY(), infoModel.getGprsX(), "0", "2");
                            } else {
                                Intent ti = new Intent(ctx, GPSNaviActivity.class);
                                ti.putExtra("mEndPoint", infoModel.getGprsY() + "/" + infoModel.getGprsX());
                                ctx.startActivity(ti);

                            }
                        } catch (Exception e) {
                            PhoneUtils.ShowToastMessage(ctx, "坐标设置错误");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        PhoneUtils.ShowToastMessage(ctx, permissions.toString() + "权限拒绝");
                    }
                });
    }

    //    导航去店
    public static void imgNavigation(final Context ctx, StoreDetailInfoModel.StoreInfoBean infoModel) {
        try {
            if (AMapUtil.isInstallByRead("com.autonavi.minimap")) {
                AMapUtil.goToNaviActivity(ctx, ctx.getString(R.string.app_name), null, infoModel.getGprsY(), infoModel.getGprsX(), "0", "2");
            } else {
                Intent ti = new Intent(ctx, GPSNaviActivity.class);
                ti.putExtra("mEndPoint", infoModel.getGprsY() + "/" + infoModel.getGprsX());
                ctx.startActivity(ti);
            }
        } catch (Exception e) {
            PhoneUtils.ShowToastMessage(ctx, "坐标设置错误");
        }
    }

    public static void onClickCallPhone(final Context ctx, final String serviceNumber) {
        Acp.getInstance(ctx).request(new AcpOptions.Builder().setPermissions(Manifest.permission.CALL_PHONE).build(),
                new AcpListener() {
                    @Override
                    public void onGranted() {
                        new UniversalDialog(ctx).builder().setTitle("门店客服:" + serviceNumber)
                                .setPositiveButton("呼叫", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent();
                                        intent.setAction(Intent.ACTION_CALL);
                                        intent.setData(Uri.parse("tel:" + serviceNumber));
                                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        ctx.startActivity(intent);

                                    }
                                }).setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }).show();


                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        PhoneUtils.ShowToastMessage(ctx, permissions.toString() + "权限拒绝");

                    }
                });
    }
    //    一键拨号
    public static void PhoneDialog(final Context ctx, final String title, String msg, String sure, String cancle) {
        new UniversalDialog(ctx).builder().setTitle(title)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + title));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        ctx.startActivity(intent);

                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    //WIFI密码
    public static void WIFIDialog(final Context ctx, final String title, String msg, String sure) {
        new WifiDialog(ctx).builder().setTitle(title).setTitle(title)
                .setMsg(msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }).show();

    }
}
